package com.example.demo1.service;

import com.example.demo1.pojo.dto.userCreateDto;
import com.example.demo1.pojo.dto.userUpdateDto;
import com.example.demo1.pojo.user;
import jakarta.validation.constraints.Digits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo1.repository.userRepository;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;

@Service
public class userService {
    @Autowired
    private static userRepository userRepository;

    public static user createUser(userCreateDto userCreateDto) {
        if(userRepository.existsByUsername(userCreateDto.getUsername())) {
            throw new RuntimeException("username already exists");
        }

        if(userCreateDto.getEmail()!=null && userRepository.existsByEmail(userCreateDto.getEmail())) {
            throw new RuntimeException("email already exists");
        }

        if(userCreateDto.getPhone()!=null && userRepository.existsByPhone(userCreateDto.getPhone())) {
            throw new RuntimeException("phone already exists");
        }

        user user = new user();
        user.setUsername(userCreateDto.getUsername());

        String encryptedPassword = DigestUtils.md5DigestAsHex(
                userCreateDto.getPassword().getBytes(StandardCharsets.UTF_8));
        user.setPassword(encryptedPassword);

        user.setEmail(userCreateDto.getEmail());
        user.setPhone(userCreateDto.getPhone());
        user.setGender(userCreateDto.getGender());
        user.setDateOfBirth(userCreateDto.getDateOfBirth());
        user.setRealName(userCreateDto.getRealName());

        return userRepository.save(user);
    }

    public static Optional<user>getUserById(Integer id){
        return userRepository.findById(id);
    }

    public static user updateUser(Integer id, userUpdateDto userDto) {
        Optional<user> userOpt = userRepository.findById(id);

        if(!userOpt.isPresent()) {
            throw new RuntimeException("user not found");
        }

        user user = userOpt.get();
        if(userDto.getEmail()!=null){
            if(userRepository.existsByEmailAndIdNot(userDto.getEmail(),id)) {
                throw new RuntimeException("email already exists");
            }
            user.setEmail(userDto.getEmail());
        }

        if(userDto.getPhone()!=null){
            if(userRepository.existsByPhoneAndIdNot(userDto.getPhone(),id)) {
                throw new RuntimeException("phone already exists");
            }
            user.setPhone(userDto.getPhone());
        }

        if(userDto.getGender()!=null){
            user.setGender(userDto.getGender());
        }

        if(userDto.getDateOfBirth()!=null){
            user.setDateOfBirth(userDto.getDateOfBirth());
        }

        if(userDto.getRealName()!=null){
            user.setRealName(userDto.getRealName());
        }

        if(userDto.getAvatarUrl()!=null){
            user.setAvatarUrl(userDto.getAvatarUrl());
        }

        return userRepository.save(user);
    }

    public static void deleteUser(Integer id) {
        if(!userRepository.existsById(id)) {
            throw new RuntimeException("user not found");
        }
        userRepository.deleteById(id);
    }

    public static List<user> getAllUsers() {
        return userRepository.findAll();
    }
}
